package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用工具类")
@RequiredArgsConstructor
public class CommonController {
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);
    /**
     * 文件上传
     * @param file
     * @return
     */
    private final AliOssUtil aliOssUtil;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传");
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            String strings = fileName.split("\\.")[0];
            String ObjectName = UUID.randomUUID().toString() + strings ;
            String filePath = aliOssUtil.upload(file.getBytes(), ObjectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return null ;
    }
}
